<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/12
 * Time: 14:11
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Search extends Base {
    /**
     * 热门搜索列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('hot_search')->count();// 获取总条数
            $lists = Db::name('hot_search')
                ->field('search_id, search_word, search_time, search_status')
                ->order('search_time desc')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $search_word=trim(input('search_word'), ' ');
            $search_type = input('search_type');
            $search_f_id = input('search_f_id');
            $search_s_id = input('search_s_id');
            $hot_goods = input('hot_goods');
            $cate_goods = input('cate_goods');
            $info = Db::table('up_hot_search')->where('search_word',$search_word)->find();
            if($info){
                return json(['code' => 100, 'msg' => '不能重复添加关键词']);
            }
            $insert = [
                'search_word' => $search_word,
                'search_type' => $search_type,
                'search_time' => date('Y-m-d H:i:s'),
            ];
            if($search_type == 1){
                if(empty($search_f_id) || empty($search_s_id) || empty($cate_goods)){
                    return json(['code' => 100, 'msg' => '请选择分类和商品']);
                }
                $insert['search_f_id'] = $search_f_id;
                $insert['search_s_id'] = $search_s_id;
                $insert['search_goods_id'] = $cate_goods;
            }
            if($search_type == 2){
                if(empty($hot_goods)){
                    return json(['code' => 100, 'msg' => '请选择商品']);
                }
                $insert['search_goods_id'] = $hot_goods;
            }
            $res = Db::table('up_hot_search')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 分类
        $f_cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->order('cate_sort asc')
            ->select();
        // 热门商品
        $hot_goods = Db::table('up_goods')
            ->where('goods_is_hot', 1)
            ->where('goods_status', 1)
            ->where('goods_del', 1)
            ->order('goods_time desc')
            ->select();
        $this->assign([
            'f_cate' => $f_cate,
            'hot_goods' => $hot_goods
        ]);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $search_id = input('id');
        $res = Db::name('hot_search')->where('search_id',$search_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $search_id = input('id');
        $total = Db::table('up_hot_search')->where('search_status', 1)->count();
        if($total >= 5){
            return json(['code' => 100, 'msg' => '最多展示5个热门搜索，请先关闭一个热门搜索']);
        }
        $res = Db::name('hot_search')->where('search_id',$search_id)->setField('search_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $search_id = input('id');
        $res = Db::name('hot_search')->where('search_id',$search_id)->setField('search_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}